package com.googlecode.mailnet.emf.netmodel.ui;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

import com.googlecode.mailnet.emf.netmodel.NetworkConfigurationModel;
import com.googlecode.mailnet.emf.netmodel.simulation.ISimulationListener;
import com.googlecode.mailnet.emf.netmodel.simulation.SimulationContext;

public class InfoPropertySection extends AbstractPropertySection implements ISimulationListener {

	public InfoPropertySection() {
		
	}

	private TableViewer viewer;
	private InfoTableContentProvider contentProvider;
	Composite composite;
	
	@Override
	public void createControls(Composite parent, TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		
		
		composite = getWidgetFactory().createFlatFormComposite(parent);
		composite.setLayout(new GridLayout(1, false));

		viewer = new TableViewer(composite, SWT.H_SCROLL | SWT.FULL_SELECTION);
		TableViewerColumn nameColumn = new TableViewerColumn(viewer, SWT.NONE);
		nameColumn.getColumn().setText("Property");
		nameColumn.getColumn().setWidth(200);
		TableViewerColumn valueColumn = new TableViewerColumn(viewer, SWT.NONE);
		valueColumn.getColumn().setText("Value");
		valueColumn.getColumn().setWidth(100);
		contentProvider = new InfoTableContentProvider();
		viewer.setContentProvider(contentProvider);
		viewer.setLabelProvider(new InfoTableLabelProvider());
        viewer.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
        viewer.getTable().setHeaderVisible(true);
	}

	/**
	 * @see org.eclipse.ui.views.properties.tabbed.ISection#shouldUseExtraSpace()
	 */
	public boolean shouldUseExtraSpace() {
		return true;
	}

	SimulationContext context = null;
	
	@Override
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);
		
		if (selection instanceof IStructuredSelection) {
			Object first = ((IStructuredSelection) selection).getFirstElement();
			if (first != null) {
				// update listener binding
				NetworkConfigurationModel configurationModel = null;
				if (first instanceof GraphicalEditPart) {
					GraphicalEditPart editPart = (GraphicalEditPart) first;
					if (editPart.resolveSemanticElement() instanceof EObject) {
						configurationModel = (NetworkConfigurationModel) ((EObject) editPart.resolveSemanticElement()).eContainer();
					}
				} else if (first instanceof ConnectionEditPart) {
					ConnectionEditPart editPart = (ConnectionEditPart) first;
					if (editPart.resolveSemanticElement() instanceof EObject) {
						configurationModel = (NetworkConfigurationModel) ((EObject) editPart.resolveSemanticElement()).eContainer();
					}
				}
				
				if (configurationModel != null) {
					SimulationContext newContext = configurationModel.getSimulationContext();
					if (newContext != context) {
						if (context != null) context.removeListener(this);
						context = newContext;
						context.addListener(this);
					}
				}
				
				// update input
				viewer.setInput(first);
			}
		}
	}
	
	@Override
	public void onChanged(SimulationContext context) {
		Display.getDefault().asyncExec(new Runnable() {
			@Override
			public void run() {
				contentProvider.refreshForced();
				viewer.refresh();
			}
		});
	}
}
